<?php
/*********************/
/*                   */
/*  文章模块         */
/*   BY voiet        */
/*   2009-7-30       */
/*                   */
/*********************/

//require_once( CORE_DIR."/model/content/mdl.article.php" );
class cmd_article extends mdl_article
{

	var $defaultCols = "title,pubtime,ifpub,orderlist,ifred,ifevent,starttime,endtime,node_id,preview";
	var $defaultOrder = array
	(
		0 => "orderlist desc,pubtime",
		1 => "desc"
	);
	
	function getcolumns( )
	{
		return array(
			"title" => array( "label" => "文章标题", "class" => "span-7" ),
			"node_id" => array( "label" => "所属栏目", "class" => "span-3", "type" => "node_name" ),
			"pubtime" => array( "label" => "上传时间", "class" => "span-3", "type" => "time" ),
			"uptime" => array( "label" => "更新时间", "class" => "span-3", "type" => "time" ),
			"ifpub" => array( "label" => "是否发布", "class" => "span-2", "type" => "bool" ),
			"ifred" => array( "label" => "是否强调", "class" => "span-2", "type" => "bool" ),
			"ifevent" => array( "label" => "是否活动", "class" => "span-2", "type" => "bool" ),
			"starttime" => array( "label" => "开始时间", "class" => "span-2", "type" => "time" ),
			"endtime" => array( "label" => "结束时间", "class" => "span-2", "type" => "time" ),
			"orderlist" => array( "label" => "排序", "class" => "span-1" ),
			"limit_level" => array(
				"label" => "允许等级",
				"class" => "span-2",
				"readonly" => true
			),
			"preview" => array( "label" => "预览", "class" => "span-3", "sql" => "article_id", "type" => "preview" )
		);
	}
	
	function _filter( $aFilter )
	{
		if ( $aFilter['title'] != "" )
		{
			$where[] = "title LIKE '".$aFilter['title']."%'";
		}
		$ndata = $this->db->select( "select node_id from sdb_sitemaps" );
		foreach ( $ndata as $key => $v )
		{
			$data[] = $v['node_id'];
		}
		$where[] = "node_id in (".implode( ",", $data ).")";
		if ( $aFilter['article_id'] )
		{
			if ( is_array( $aFilter['article_id'] ) )
			{
				foreach ( $aFilter['article_id'] as $id )
				{
					if ( $id != "_ANY_" )
					{
						$aId[] = intval( $id );
					}
				}
				if ( 0 < count( $aId ) )
				{
					$where[] = "article_id IN (".implode( ",", $aId ).")";
				}
			}
			else
			{
				$where[] = "article_id=".$aFilter['article_id'];
			}
		}
		if ( $aFilter['node_id'] )
		{
			if ( is_array( $aFilter['node_id'] ) )
			{
				foreach ( $aFilter['node_id'] as $catid )
				{
					if ( $catid != "_ANY_" )
					{
						$aCats[] = intval( $catid );
					}
				}
				if ( 0 < count( $aCats ) )
				{
					$where[] = "node_id IN (".implode( ",", $aCats ).")";
				}
			}
			else
			{
				$where[] = "node_id=".$aFilter['node_id'];
			}
		}
		unset( $aFilter['node_id']);
		unset( $aFilter['title']);
		if ( isset( $filter['ifeventover'] ) && $filter['ifeventover'] == 1 )
		{
			$curTime = time( );
			$where[] = "endtime<".$curTime." and endtime > 0 ";
		}
		if ( isset( $filter['ifeventing'] ) && $filter['ifeventing'] == 1 )
		{
			$curTime = time( );
			$where[] = "endtime>=".$curTime." and starttime<".$curTime ;
		}
		if ( 0 < count( $where ) )
		{
			return implode( " AND ", $where )." AND ".shopobject::_filter( $aFilter );
		}
		return shopobject::_filter( $aFilter );
	}
	
	function savearticle( $aData )
	{
		if ( !$aData['article_file'] )
		{
			unset( $aData['article_file']);
		}
		$aData['limit_level'] = implode( ",", $aData['limit_level'] );
		$aData['starttime'] = strtotime( $aData['starttime'] );
		$aData['endtime'] = strtotime( $aData['endtime'] );
		$storager =& $this->system->loadmodel( "system/storager" );
		if ( $_FILES['article_file']['name'] )
		{
			$aData['filename'] = $storager->save_upload( $_FILES['article_file'], "article", array(
				$aData['article_id'],
				"article"
			) );
		}
		if ( $aData['article_id'] )
		{
			$aData['uptime'] = time( );
			$aRs = $this->db->query( "SELECT * FROM sdb_articles WHERE article_id=".$aData['article_id'] );
			$sSql = $this->db->getupdatesql( $aRs, $aData );
			return !$sSql || $this->db->exec( $sSql );
		}
		$aData['pubtime'] = time( );
		$aRs = $this->db->query( "SELECT * FROM sdb_articles WHERE 0=1" );
		$sSql = $this->db->getinsertsql( $aRs, $aData );
		if ( $this->db->exec( $sSql ) )
		{
			return $this->db->lastinsertid( );
		}
		return false;
		
	}
	
	
	function isallow( $alimit_level, $mlv, $num = 1 )
	{
		$aArticleLimitLevel = explode( ",", $alimit_level );
		if ( $alimit_level == NULL || $alimit_level == '' || empty($alimit_level))
		{
			return true;
		}
		elseif ( in_array( $mlv, $aArticleLimitLevel ) )
		{
			return true;
		}
		else return false;
	}
	
	function getcatname($node_id )
	{
		$sSql = "SELECT title from sdb_sitemaps where node_id=".$node_id;
		return $aTemp = $this->db->selectrow( $sSql );
	}
	
	function updateclick($article_id,$nums )
	{
		$aData['click'] = $nums;
		$aData['article_id'] = $article_id;
		$aRs = $this->db->query( "SELECT * FROM sdb_articles WHERE article_id=".$aData['article_id'] );
		$sSql = $this->db->getupdatesql( $aRs, $aData );
		return !$sSql || $this->db->exec( $sSql );
	}
		
	

}

?>